//
//  MYUserAccount.swift
//  MYWeiBo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

class MYUserAccount: NSObject,NSCoding{
 ///模型变量
    //用户授权的唯一票据
    var access_token : String?
    //access_token生命周期的时间
    var expires_in : NSTimeInterval = 0 {
        didSet {
            expirsDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期时间，方便来比较
    var expirsDate : NSDate?
    //授权用户的uid
    var uid : String?
    //用户的昵称
    var screen_name : String?
    //用户的头像，180 * 180
    var avatar_large : String?
    
    
    /**
     * 重写init方法，通过字典来创建模型数据
     */
    init(dict: [String: AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)

    }
    override func setValue(value: AnyObject?, forKey key: String) {
        super.setValue(value, forKey: key)
        
    }
 
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
    }
    /**
    *  重写要被打印的属性description
    */
    override var description:String{
//      get {
//        
//            return "用户账户：access_token:\(access_token),expires_in:\(expires_in),uid:\(uid)"
//          }
        let keys = ["access_token","expires_in","expirsDate","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //mark:归档和解档
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expirsDate, forKey: "expirsDate")
        aCoder.encodeObject(uid, forKey: "uid")
        //用户信息
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as?String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as?String
        expirsDate = aDecoder.decodeObjectForKey("expirsDate") as?NSDate
        //用户信息
        screen_name = aDecoder.decodeObjectForKey("screen_name") as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as?String
        
    }
    
}






